import navToNext from '../../../../wxjs/navToNext.js'
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  balanceAmout:0,
  dialogValue:{},
  amount: null,
  array: ['微信', '支付宝'],
  objectArray: [{ name: '微信', status: 1 }, { name: '支付宝', status: 2 }],
  falseWord: {
    amount:'提现金额不为空！',
    username:'提现人名称不为空！',
    withdrawType:'提现方式不为空！',
    phone:'提现帐号不为空！',
  },
};

var currentPage = {
  data,
  onLoad,
  bindPickerChange,
  formSubmit,
  getWithdrawal,
  getAmount,
  dialog,
  addWithdrawApplication,
};

var page = Object.assign({}, navToNext, currentPage);

Page(page);


function onLoad(options) {
  const that = this;
  console.log(options)
  that.setData({
    balanceAmout:options.amout
  })
}

// 全部提现
function getWithdrawal(){
  const that=this;
  that.setData({
    amount: that.data.balanceAmout
  })
}
function getAmount(e){
  const that=this;
  var amount = e.detail.value;
  console.log('amount > that.data.balanceAmout', amount > that.data.balanceAmout)
  if (Number(amount) > Number(that.data.balanceAmout)){
    amount = that.data.balanceAmout
  }
  that.setData({
    amount: amount
  })
}
// 收账方式
function bindPickerChange(e) {
  const that=this;
  console.log(that.data.objectArray[e.detail.value].status,'picker发送选择改变，携带值为', e.detail.value)
  that.setData({
    index: e.detail.value
  })
}
// 确认提现
function formSubmit(e){
  // console.log('form发生了submit事件，携带数据为：', e.detail.value)
  const that=this;
  var param = e.detail.value;
  param.amount = that.data.amount;
  param.withdrawType = param.withdrawType||1;
  console.log(param)
  for (var key in param){
    // console.log(key, param[key])
    if (!param[key]){
      that.dialog(that.data.falseWord[key]);
      return;
    }
  }
  that.addWithdrawApplication(param);
}
function dialog(title) {
  var dialogValue = {
    showDialog: true,
    backgroundColor: '#2D2D2D', //背景颜色
    title: '提示', //标题
    titleColor: '#fff',  //标题颜色
    content: title,  //主体内容
    contentColor: '#CECECE',

    confirmText: '确定',  //右边按钮文字
    confirmColor: '',  //右边按钮颜色
    // 右边按钮回调函数
    confirm: function (res) {
      // console.log('111111111111111')
    },
  }
  this.setData({
    dialogValue: dialogValue
  })
}

function addWithdrawApplication(param) {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.addWithdrawApplication,
    data: param,
    cancelToast: true,
    success: function (res) {
      wx.navigateTo({
       // url: `${app.xhx_route.withdrawalSuccess}?uuid=${uuid}`,
        url: `${app.xhx_route.withdrawalSuccess}`,
      })
    },
    fail: function (res) {
      // app.middleware.showToast(res && res.msg || "请求失败");
      that.dialog(res && res.msg || "请求失败");
    }
  })
}


